//
//  TTTab.m
//  Tint
//
//  Created by Kevin Wu on 4/13/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TTTab.h"
#import "TTTabBar.h"


@interface TTTab (Private)

- (void)buttonTouchDown:(id)sender;
- (void)buttonTouchUpInside:(id)sender;
- (void)buttonTouched:(id)sender;

@end

@implementation TTTab (Private)

- (void)buttonTouchDown:(id)sender {
    self.state = UIControlStateHighlighted;
    [_tabbar updateByTab:self];
}

- (void)buttonTouchUpInside:(id)sender {
    self.state = UIControlStateSelected;
    [_tabbar updateByTab:self];
}

- (void)buttonTouched:(id)sender {
    [_tabbar update];
}

@end




@implementation TTTab


#pragma mark - Accessors

@synthesize tabbar = _tabbar;


@synthesize state = _state;

- (void)setState:(UIControlState)state {
    if (state == UIControlStateNormal || 
        state == UIControlStateHighlighted || 
        state == UIControlStateSelected) {
        
        _state = state;
        
        switch (_state) {
            case UIControlStateHighlighted:
                [self setHighlightedStyle];
                break;
                
            case UIControlStateSelected:
                [self setSelectedStyle];
                break;
                
            default:
                [self setNormalStyle];
                break;
        }
        
    }
}


@synthesize normalBackgroundImage = _normalBackgroundImage;

- (void)setNormalBackgroundImage:(UIImage *)normalBackgroundImage {
    TTRetain(normalBackgroundImage, _normalBackgroundImage);
    self.state = _state;
}


@synthesize hilightedBackgroundImage = _hilightedBackgroundImage;

- (void)setHilightedBackgroundImage:(UIImage *)hilightedBackgroundImage {
    TTRetain(hilightedBackgroundImage, _hilightedBackgroundImage);
    self.state = _state;
}


@synthesize title = _title;

- (void)setTitle:(NSString *)title {
    TTCopy(title, _title);
    self.state = _state;
}


@synthesize button = _button;



#pragma mark - Memory

- (id)init {
    self = [super init];
    if (self) {
        
        _tabbar = nil;
        
        _state = UIControlStateNormal;
        [self setNormalStyle];
        
        
        _backgroundImageView = [[UIImageView alloc] init];
        _backgroundImageView.backgroundColor = [UIColor clearColor];
        _backgroundImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:_backgroundImageView];
        
        
        _button = [[UIButton alloc] init];
        [self addSubview:_button];
        [_button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
        [_button addTarget:self action:@selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
        
        UIControlEvents events = 
//        UIControlEventTouchDragInside | 
        UIControlEventTouchDragOutside | 
        UIControlEventTouchDragEnter | 
        UIControlEventTouchDragExit | 
        UIControlEventTouchUpOutside | 
        UIControlEventTouchCancel | 
        UIControlEventTouchDownRepeat;
        [_button addTarget:self action:@selector(buttonTouched:) forControlEvents:events];
    }
    return self;
}

- (void)dealloc {
    _tabbar = nil;
    _state = UIControlStateNormal;
    
    TTRelease(_normalBackgroundImage);
    TTRelease(_hilightedBackgroundImage);
    
    TTRelease(_button);
    TTRelease(_backgroundImageView);
    [super dealloc];
}



#pragma mark - Public

+ (TTTab *)tabWithNormalBackgroundImage:(UIImage *)normalBackgroundImage hilightedBackgroundImage:(UIImage *)hilightedBackgroundImage title:(NSString *)title {
    TTTab *tab = [[self alloc] init];
    tab.normalBackgroundImage = normalBackgroundImage;
    tab.hilightedBackgroundImage = hilightedBackgroundImage;
    tab.title = title;
    return [tab autorelease];
}

- (void)setNormalStyle {
    _backgroundImageView.image = _normalBackgroundImage;
    _button.normalTitle = _title;
}

- (void)setHighlightedStyle {
    _backgroundImageView.image = _hilightedBackgroundImage;
    _button.normalTitle = _title;
}

- (void)setSelectedStyle {
    [self setHighlightedStyle];
}

- (CGSize)idealSize {
    return TTSizeZero();
}



#pragma mark - UIView

- (void)layoutSubviews {
    [super layoutSubviews];
    [_button setFrameIfNeeded:self.bounds];
    [_backgroundImageView setFrameIfNeeded:self.bounds];
}

@end
